﻿using LYL.Common.RT.Text;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using WeixinSB.Model;
using Windows.Data.Json;
using Windows.Storage;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

// The data model defined by this file serves as a representative example of a strongly-typed
// model.  The property names chosen coincide with data bindings in the standard item templates.
//
// Applications may use this model as a starting point and build on it, or discard it entirely and
// replace it with something appropriate to their needs. If using this model, you might improve app 
// responsiveness by initiating the data loading task in the code behind for App.xaml when the app 
// is first launched.

namespace WeixinSB.Data
{
    public class TemplateDataST
    {
        public static WeixinSbModel _single;
        public static WeixinSbModel Single
        {
            get
            {
                return _single;
            }
            set
            {
                _single = value;
            }
        }

        public static async Task<WeixinSbModel> Get()
        {
            var list = await TemplateDataSource.GetItemAsync();

            if (_single == null && list != null && list.Count > 0)
            {
                _single = list[0];
            }


            return _single;
        }
    }

    /// <summary>
    /// Creates a collection of groups and items with content read from a static json file.
    /// 
    /// SampleDataSource initializes with data read from a static json file included in the 
    /// project.  This provides sample data at both design-time and run-time.
    /// </summary>
    public class TemplateDataSource
    {
        private static TemplateDataSource _sampleDataSource = new TemplateDataSource();

        private List<WeixinSbResponse> _groups = new List<WeixinSbResponse>();
        public List<WeixinSbResponse> Groups
        {
            get { return this._groups; }
        }

        public static async Task<List<WeixinSbResponse>> GetGroupAsync()
        {
            await _sampleDataSource.GetSampleDataAsync();

            return _sampleDataSource._groups;
        }


        public static async Task<List<WeixinSbModel>> GetItemAsync()
        {
            var list = new List<WeixinSbModel>();

            await _sampleDataSource.GetSampleDataAsync();

            var result = _sampleDataSource.Groups.Select(p => p.DataModel);

            foreach (var item in result)
            {
                foreach (var sub in item)
                {
                    list.Add(sub);
                }
            }

            return list;
        }


        public static async Task<WeixinSbModel> GetItemAsync(int uniqueId)
        {
            await _sampleDataSource.GetSampleDataAsync();
            // Simple linear search is acceptable for small data sets
            var matches = _sampleDataSource.Groups.Where(p => p.Id == uniqueId).Select(p => p.DataModel);
            if (matches.Count() == 1) return matches.First()[0];
            return null;
        }

        private async Task GetSampleDataAsync()
        {
            if (this._groups.Count != 0)
                return;

            try
            {
                Uri dataUri = new Uri("ms-appx:///DataModel/TemplateData.json");
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
                string jsonText = await FileIO.ReadTextAsync(file);
                var list = JsonHelper.JsonToObj<List<WeixinSbResponse>>(jsonText);
                if (list != null && list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        _groups.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}